package hero_info

import (
	"go_server/game_server/api/service"
	"go_server/game_server/dataconfig/config_manager"
	"go_server/game_server/event_manager"
	"go_server/game_server/event_manager/event_data"
	"go_server/zinx/zlog"
	"math"
)

type Hero_info_service struct {
	service.AbsService
	eventMethods map[event_data.EventType]func(event_data.IEvent)

	//----变量分隔线----------------------------------------------------------
	isUseRedis    bool //是否使用Redis
	fuseCellCount int  //融合细胞数量
	recombinCell  int  //重组细胞数量

	//----英雄分隔线----------------------------------------------------------
	router_AddHero           *Router_AddHero
	router_DelHero           *Router_DelHero
	router_GetHeros          *Router_GetHeros
	router_UpLvHero          *Router_UpLvHero
	router_UpStarHero        *Router_UpStarHero
	router_UnlockHero        *Router_UnlockHero
	router_UniToExcFragments *Router_UniToExcFragments

	//----细胞分隔线----------------------------------------------------------
	router_AddCell        *Router_AddCell
	router_EquipCell      *Router_EquipCell
	router_FuseCell       *Router_FuseCell
	router_UpdateCellList *Router_UpdateCellList
	router_RecombinCell   *Router_RecombinCell
	router_UnEquipCell    *Router_UnEquipCell
}

var (
	Hero_info_serviceObj *Hero_info_service
)

func (s *Hero_info_service) GetModuleName() string {
	return "Hero_info_service"
}
func (s *Hero_info_service) GetEventMap() map[event_data.EventType]func(event_data.IEvent) {
	return s.eventMethods
}
func (s *Hero_info_service) GetEventHandleFunc(eventType event_data.EventType) (func(event_data.IEvent), bool) {
	eventFunc, ok := s.eventMethods[eventType]
	if !ok {
		zlog.Errorf("GetEventHandleFunc Err!!! method not found!! eventType: %d", eventType)
		return nil, false
	}
	return eventFunc, true
}

func init() {
	Hero_info_serviceObj = &Hero_info_service{
		eventMethods: make(map[event_data.EventType]func(event_data.IEvent)),

		//----变量分隔线----------------------------------------------------------
		isUseRedis:    false,
		fuseCellCount: 4, //融合细胞数量
		recombinCell:  2, //重组细胞数量

		//----英雄分隔线----------------------------------------------------------
		router_AddHero:           &Router_AddHero{},
		router_DelHero:           &Router_DelHero{},
		router_GetHeros:          &Router_GetHeros{},
		router_UpLvHero:          &Router_UpLvHero{},
		router_UpStarHero:        &Router_UpStarHero{},
		router_UnlockHero:        &Router_UnlockHero{},
		router_UniToExcFragments: &Router_UniToExcFragments{},
		//----细胞分隔线----------------------------------------------------------
		router_AddCell:        &Router_AddCell{},
		router_EquipCell:      &Router_EquipCell{},
		router_FuseCell:       &Router_FuseCell{},
		router_UpdateCellList: &Router_UpdateCellList{},
		router_RecombinCell:   &Router_RecombinCell{},
		router_UnEquipCell:    &Router_UnEquipCell{},
	}
	//event_manager.EventManagerObj.AddServiceEvent(event_data.RoleCreateEvent, onReceiveRoleCreateEvent, Hero_info_serviceObj)
	event_manager.EventManagerObj.AddServiceEvent(event_data.LoginEvent, onReceiveLoginEvent, Hero_info_serviceObj)
}

func onReceiveLoginEvent(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}
	Hero_info_serviceObj.S2C_AddDefHeros(conn, roleId)
	Hero_info_serviceObj.S2C_GetCells(conn, roleId)
}

func (s *Hero_info_service) GetFuseCellCount() int {
	return s.fuseCellCount
}
func (s *Hero_info_service) GetRecombinCellCount() int {
	return s.recombinCell
}

const (
	celltype_None     = 0 //细胞类型-无
	celltype_Fusion   = 1 //细胞类型-融合
	celltype_Recombin = 2 //细胞类型-重组
)

// 获得下一等级指定类型的细胞组
// map[int32：细胞CfgId]int32:细胞权重
// 返回【值1：curLv等级对应的fusion细胞,值2：下一等级的细胞】
func (s *Hero_info_service) onNextLevelFuseCells(curlv int32, fusion int32) (map[int32]int32, bool, map[int32]int32, bool) {
	rstCurFuseCells := make(map[int32]int32)
	rstNextFuseCells := make(map[int32]int32)
	cfgs := config_manager.GetTotal_StcTableHeroCell_SheetCell()
	nextlv := curlv + 1
	if cfgs != nil {
		for i := 0; i < len(cfgs); i++ {
			if v, ok := config_manager.GetStcTableHeroCell_SheetCell(cfgs[i]); ok {
				if v.Level == nextlv { //下一等级类型
					rstNextFuseCells[v.Id] = v.Weight
				}
				if v.Fusion == fusion && v.Level == curlv { //当前等级可融合类型
					rstCurFuseCells[v.Id] = v.Weight
				}
			}
		}
	}
	return rstCurFuseCells, len(rstCurFuseCells) > 0, rstNextFuseCells, len(rstNextFuseCells) > 0
}

// 获得下一等级可融合的细胞组 map[int32：细胞CfgId]int32:细胞权重
func (s *Hero_info_service) GetNextLevelFuseCells(curlv int32) (map[int32]int32, bool, map[int32]int32, bool) {
	return s.onNextLevelFuseCells(curlv, celltype_Fusion)
}

// 获得下一等级可重组的细胞组 map[int32：细胞CfgId]int32:细胞权重
func (s *Hero_info_service) GetNextLevelRecombinCells(curlv int32) (map[int32]int32, bool, map[int32]int32, bool) {
	return s.onNextLevelFuseCells(curlv, celltype_Recombin)
}

// 获得英雄最高星级
func (s *Hero_info_service) GetHeroMaxStar() int32 {
	maxstar := int32(0)
	cfgs := config_manager.GetTotal_StcTableHero_SheetStar()
	if cfgs != nil {
		for i := 0; i < len(cfgs); i++ {
			if v, ok := config_manager.GetStcTableHero_SheetStarConfig(cfgs[i]); ok {
				maxstar = int32(math.Max(float64(maxstar), float64(v.Id)))
			}
		}
	}
	return maxstar
}

// 获得英雄最高等级
func (s *Hero_info_service) GetHeroMaxLevel() int32 {
	maxlevel := int32(0)
	cfgs := config_manager.GetTotal_StcTableHero_SheetLevel()
	if cfgs != nil {
		for i := 0; i < len(cfgs); i++ {
			if v, ok := config_manager.GetStcTableHero_SheetLevelConfig(cfgs[i]); ok {
				maxlevel = int32(math.Max(float64(maxlevel), float64(v.Id)))
			}
		}
	}
	return maxlevel
}

// 获得细胞最高等级
func (s *Hero_info_service) GetCellMaxLevel() int32 {
	maxlevel := int32(0)
	cfgs := config_manager.GetTotal_StcTableHeroCell_SheetCell()
	if cfgs != nil {
		for i := 0; i < len(cfgs); i++ {
			if v, ok := config_manager.GetStcTableHeroCell_SheetCell(cfgs[i]); ok {
				maxlevel = int32(math.Max(float64(maxlevel), float64(v.Level)))
			}
		}
	}
	return maxlevel
}

// 获得英雄最大装备细胞数
func (s *Hero_info_service) GetHeroEquipCellMaxCount() int32 {
	return 2
}
